<!doctype html>
<meta charset="utf-8">
<title>Async Clipboard input type validation tests</title>
<link rel="help" href="https://w3c.github.io/clipboard-apis/#async-clipboard-api">
<body>Body needed for test_driver.click()</body>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="../resources/testdriver.js"></script>
<script src="../resources/testdriver-vendor.js"></script>
<script src="resources/user-activation.js"></script>
<script>

// Permissions are required in order to invoke navigator.clipboard functions in
// an automated test.
async function getPermissions() {
  await tryGrantReadPermission();
  await tryGrantWritePermission();
  await waitForUserActivation();
}

test(() => {
  assert_not_equals(navigator.clipboard, undefined);
  assert_true(navigator.clipboard instanceof Clipboard);
  assert_equals(navigator.clipboard, navigator.clipboard);
}, 'navigator.clipboard exists');

promise_test(async () => {
  await getPermissions();
  const blob = new Blob(['hello'], {type: 'text/plain'});
  const item = new ClipboardItem({'text/plain': blob});

  await navigator.clipboard.write([item]);
}, 'navigator.clipboard.write([text/plain ClipboardItem]) succeeds');

promise_test(async t => {
  await getPermissions();
  const blob1 = new Blob(['hello'], {type: 'text/plain'});
  const blob2 = new Blob(['world'], {type: 'text/plain'});

  const item1 = new ClipboardItem({'text/plain': blob1});
  const item2 = new ClipboardItem({'text/plain': blob2});

  await promise_rejects_dom(t, "NotAllowedError",
      navigator.clipboard.write([item1, item2]));
}, 'navigator.clipboard.write([>1 ClipboardItems]) fails (not implemented)');

promise_test(async t => {
  await getPermissions();
  await promise_rejects_js(t, TypeError, navigator.clipboard.write());
}, 'navigator.clipboard.write() fails (expect [ClipboardItem])');

promise_test(async t => {
  await getPermissions();
  await promise_rejects_js(t, TypeError, navigator.clipboard.write(null));
}, 'navigator.clipboard.write(null) fails (expect [ClipboardItem])');

promise_test(async t => {
  await getPermissions();
  await promise_rejects_js(t, TypeError,
                           navigator.clipboard.write('Bad string'));
}, 'navigator.clipboard.write(DOMString) fails (expect [ClipboardItem])');

promise_test(async t => {
  await getPermissions();
  const blob = new Blob(['hello'], {type: 'text/plain'});
  await promise_rejects_js(t, TypeError, navigator.clipboard.write(blob));
}, 'navigator.clipboard.write(Blob) fails (expect [ClipboardItem])');

promise_test(async () => {
  await getPermissions();
  await navigator.clipboard.writeText('New clipboard text');
}, 'navigator.clipboard.writeText(DOMString) succeeds');

promise_test(async t => {
  await getPermissions();
  await promise_rejects_js(t, TypeError,
                           navigator.clipboard.writeText());
}, 'navigator.clipboard.writeText() fails (expect DOMString)');

promise_test(async () => {
  await getPermissions();
  const item = new ClipboardItem({'text/plain': 'test'});
  await navigator.clipboard.write([item]);
}, 'navigator.clipboard.write({string : DOMString}) succeeds');

promise_test(async () => {
  await getPermissions();
  const fetched = await fetch('../clipboard-apis/resources/greenbox.png');
  const image = await fetched.blob();
  const item = new ClipboardItem({'image/png': image});

  await navigator.clipboard.write([item]);
}, 'navigator.clipboard.write({string : image/png Blob}) succeeds');

promise_test(async() => {
  await getPermissions();
  const fetched = await fetch('../clipboard-apis/resources/greenbox.png');
  const image = await fetched.blob();
  const item = new ClipboardItem({
    'text/plain': new Blob(['first'], {type: 'text/plain'}),
    'image/png': image});

  await navigator.clipboard.write([item]);
}, 'navigator.clipboard.write([text + png] succeeds');

promise_test(async t => {
  await getPermissions();
  const item = new ClipboardItem({'image/png': 'not an image'});
  await promise_rejects_js(t, TypeError, navigator.clipboard.write([item]));
}, 'navigator.clipboard.write(image/png DOMString) fails');

promise_test(async () => {
  await getPermissions();
  const result = await navigator.clipboard.read();
  assert_true(result instanceof Object);
  assert_true(result[0] instanceof ClipboardItem);
}, 'navigator.clipboard.read() succeeds');

promise_test(async () => {
  await getPermissions();
  const result = await navigator.clipboard.readText();
  assert_equals(typeof result, 'string');
}, 'navigator.clipboard.readText() succeeds');

promise_test(async () => {
  await getPermissions();
  const promise_blob = Promise.resolve(new Blob(['hello'], {type: 'text/plain'}));
  const item = new ClipboardItem({'text/plain': promise_blob});

  await navigator.clipboard.write([item]);
}, 'navigator.clipboard.write(Promise<Blob>) succeeds');

promise_test(async () => {
  await getPermissions();
  const promise_text_blob = Promise.resolve(new Blob(['hello'], {type: 'text/plain'}));
  const promise_html_blob = Promise.resolve(new Blob(["<p style='color: red; font-style: oblique;'>Test</p>"], {type: 'text/html'}));
  const item = new ClipboardItem({'text/plain': promise_text_blob, 'text/html': promise_html_blob});

  await navigator.clipboard.write([item]);
}, 'navigator.clipboard.write(Promise<Blob>s) succeeds');

</script>
